var city1 = $("#city");

var tianjia = $("#tianjia");

var data1; //临时储存接口返回数据

var pages = 0;

var opn = true;

var map = new AMap.Map("container123", {
  resizeEnable: true,
});
AMap.plugin("AMap.Geolocation", function () {
  var geolocation = new AMap.Geolocation({
    enableHighAccuracy: true, //是否使用高精度定位，默认:true
    timeout: 10000, //超过10秒后停止定位，默认：5s
    buttonPosition: "RB", //定位按钮的停靠位置
    buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
    zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
  });
  map.addControl(geolocation);
  geolocation.getCurrentPosition(function (status, result) {
    if (status == "complete") {
      onComplete(result);
    } else {
      onError(result);
    }
  });
});

function onComplete(data) {
  abc(data.addressComponent.city);
}
function onError(data) {
  console.log(data.message);
}

tiaozhuan = function (index) {
  $("#area1").html(
    data1.newslist[index].area +
      '<img src="img/' +
      data1.newslist[index].weatherimg +
      '" alt="" >'
  );
  $("#week").html(data1.newslist[index].week);
  $("#weather1").html("天气变化" + data1.newslist[index].weather);
  $("#real").html("当前气温：" + data1.newslist[index].real);
  $("#wencha").html(
    "最低气温：" +
      data1.newslist[index].lowest +
      "<br>最高气温：" +
      data1.newslist[index].highest
  );
  $("#wind").html("风向：" + data1.newslist[index].wind);
  $("#windsc").html("风力等级：" + data1.newslist[index].windsc);
  $("#moonrise").html(
    "日照时间：" +
      data1.newslist[index].moonrise +
      "-" +
      data1.newslist[index].moondown
  );
  $("#vis").html("能见度：" + data1.newslist[index].vis);
  $("#tips").html("建议：" + data1.newslist[index].tips);
  $("#date").html("日期：" + data1.newslist[index].date);
};

abc = function (city) {
  $.ajax({
    url:
      "http://api.tianapi.com/tianqi/index?key=7db75f0a8b9e89d93762b622eea1a8f0&city=" +
      city,
    type: "GET",
    success: function (data) {
      data1 = data;
      if (data1.code != 200) {
        alert("无该城市");
        $("#city").val("");
        $("#city").focus();
      } else {
        pages = 0;
        console.log(data1);
        console.log(data1.newslist[0].weather);
        tiaozhuan(pages);
        console.log($(".xkx"));
        console.log($(".xkx").length);
        console.log(opn);
        opn = true;
        for (let i = 0; i < $(".xkx").length; i++) {
          console.log($(".xkx:eq(" + i + ")").text());
          if ($(".xkx").eq(String(i)).text() == $("#city").val()) {
            opn = false;
            break;
          }
        }
        console.log(opn);
        if (opn)
          $("#container_right").html(
            $("#container_right").html() +
              '<div><button class="xkx">' +
              $("#city").val() +
              '</button><button class="delete">删除</button></div><br>'
          );
      }
    },
    error: function () {
      console.log("失败");
    },
    dataType: "json",
  });
};
$("#tianjia").click(function () {
  abc($("#city").val());
  console.log($("container_right").text());
});
$("body").on("click", ".xkx", function () {
  abc($(this).text());
});
$("body").on("click", ".delete", function () {
  $(this).parent().remove();
});
$("#button_right").click(function () {
  if (pages != 6) {
    pages++;
  } else {
    pages = 0;
  }
  tiaozhuan(pages);
});
$("#button_left").click(function () {
  if (pages != 0) {
    pages--;
  } else {
    pages = 6;
  }
  tiaozhuan(pages);
});
